package zj.com.app;

import android.app.Activity;
import android.app.Application;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.support.v7.app.NotificationCompat;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import org.xutils.x;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import zj.com.crash.UncaughtExceptionHandler;
import zj.com.model.AuditLog;
import zj.com.model.Dzg;
import zj.com.model.DzgDetail;
import zj.com.model.DzgDetailDB;
import zj.com.model.DzgInspectItemDB;
import zj.com.model.DzgInspectTask;
import zj.com.model.GsTj;
import zj.com.model.GsTjOrg;
import zj.com.model.InspectItem;
import zj.com.model.JcdInfoDB;
import zj.com.model.Org;
import zj.com.model.PgScan;
import zj.com.model.PrjTj;
import zj.com.model.Project;
import zj.com.model.User;
import zj.com.util.LogUtils;
import zj.com.util.NotifyUtil;
import zj.com.zj.BuildConfig;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/17.
 */
public class ZjApplication extends Application {


    public static  int type = 0;// 0 检查单类型  1 公司类型 2项目类型  3 扣分  4  隐患级别  5 专业  6 施工
    private List<Activity> mList = new LinkedList<Activity>();

    public static List<InspectItem> inspectItems = new ArrayList<>();
    public static PgScan pgScan;
    public static JcdInfoDB jcd ;
    public static boolean isUpdateJcd = false;//是否是修改检查单
    private static ZjApplication instance;
    public static String jpushRegisterID;


    public static JcdInfoDB dqrJcdInfo;
    public static int isAcc = -1;//0 代表待确认陪同
    public static User checkUser;



    //待整改的
    public static DzgDetailDB dzgDetailDB;
    public static DzgInspectItemDB dzgInspectItemDB;
    public static DzgDetail dzgDetail;


    //公司统计
    public static List<GsTj>  gstjList;
    //项目统计
    public static List<PrjTj> prjList;

    //公司列表
    public static List<Org> orgList;
    public static Map<String,List<Project>> projectMap = new HashMap<>();


    public static List<AuditLog> auditLogList;
    //构造方法
    public ZjApplication(){}
    //实例化一次
    public synchronized static ZjApplication getInstance(){
        if (null == instance) {
            instance = new ZjApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        Fresco.initialize(getApplicationContext());
        JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush
        jpushRegisterID =  JPushInterface.getRegistrationID(this);
        LogUtils.getInstance().error("jpushRegisterID="+jpushRegisterID);

        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG);
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(getApplicationContext()));

        NotifyUtil notif = NotifyUtil.getInstance(this);
        notif.initService(this);
        notif.initNotify();

        initImageLoader(this);
    }


    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }
    //关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity:mList) {


                LogUtils.getInstance().error(activity.getLocalClassName());
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }

}
